<?php

include_once("SweetTestSuite.php");

class SweetTestRunner
{
	/**
	 * Suite to be acted upon
	 *
	 * @var SweetTestSuite
	 */
	private $sweetTestSuite;

	/**
	 * Constructor
	 * 
	 * @param SweetTestSuite $sweetTestSuite
	 */
	public function __construct(SweetTestSuite $sweetTestSuite = null)
	{
		if($testSuite == null)
			$this->sweetTestSuite = new SweetTestSuite();
		else
			$this->sweetTestSuite = $testSuite;
	}

	/**
	 * Generates a List of Files for a Path Extension and Exclusion Pattern
	 * 
	 * @param string $path
	 * @param array(string) $extension
	 * @param array(string) $excluded
	 * @return string
	 */
	private function getFiles($path,$extension = null,$excluded = null)
	{
		$files = array();
		$directory = scandir($path);

		if($extension == null)
		$extension = array(".*[tT]est.php");

		if($excluded == null)
		$excluded = array(".","..");
		else
		$excluded = array_merge($excluded,array(".",".."));

		foreach($directory as $file)
		{
			if(in_array($file,$excluded))
			continue;

			$pathFile = $path.'/'.$file;

			if(is_dir($pathFile) && is_readable($pathFile))
			{
				$files = array_merge($files,$this->getFiles($pathFile,$extension,$excluded));
			} else if(is_file($pathFile) && is_readable($pathFile))
			{
				foreach($extension as $regex)
				{
					if(ereg($regex,$pathFile))
					{
						$files[] = $pathFile;
					}
				}
					
			} else {
				throw new Exception("Something Bad! ".$file);
			}
		}

		return $files;
	}

	/**
	 * Adds a path to the Suite
	 * @param string $path
	 */
	public function addPath($path)
	{
		$files = $this->getFiles($path);

		foreach($files as $file)
		{
			include_once($file);
			$file = ereg_replace(".php","",ereg_replace(".*/","",$file));
			$this->sweetTestSuite->addTestClass($file);
		}
	}

	/**
	 * Adds a class to the Suite
	 * @param string $class
	 */	
	public function addClass($class)
	{
		if(is_file($class))
		{
			include_once($class);
			$class = ereg_replace(".php","",ereg_replace(".*/","",$class));
			try {
				$this->sweetTestSuite->addTestClass($class);
			} catch (Exception $e)
			{
				throw new Exception("Cannot find class ".$class);
			}
			return;
		}

		try {
			$this->sweetTestSuite->addTestClass($class);
		} catch(Exception $e)
		{
			$file = $this->getFiles("./",array(".*".$class.".php"));
			if(sizeof($file) == 1)
			{
				include_once($file[0]);
				$this->sweetTestSuite->addTestClass($class);
			} else {
				throw new Exception("Cannot find class ".$class);
			}
		}
	}
	
	/**
	 * Runs the set of tests for the suite
	 * @param boolean $timeout
	 */
	public function run($timeout = true)
	{
		$this->sweetTestSuite->run($timeout);
	}

	/**
	 * gets the current suite
	 *
	 * @return SweetTestSuite
	 */
	public function getSuite()
	{
		return $this->sweetTestSuite;
	}

	/**
	 * toString
	 *
	 * @return string
	 */
	public function __toString()
	{
		return $this->sweetTestSuite->__toString();
	}
}

?>